package com.cndragon.baby.notification
{
	import flash.utils.Dictionary;

	/**
	 * 负责管理命令，一个EventId只能指向一个Command，一个Command可以被多个EventId所指向
	 * @author	bluesliu
	 */	
	public class CommandMgr
	{
		private var commandReferenceList:Array;
		private var commandInstanceList:Dictionary;
		
		static private var _instance:CommandMgr;
		
		public function CommandMgr(s:S)
		{
			commandReferenceList = [];
			commandInstanceList = new Dictionary();
		}
		
		public static function getInstance():CommandMgr
		{
			if(_instance==null)
			{
				_instance = new CommandMgr(new S());
			}
			return _instance;
		}
		
		
		
		/**
		 * 注册一个Command，当有发送此notificationID时，会执行CommandClass的execute方法
		 * @param notificationID
		 * @param CommandClass
		 * 
		 */		
		public function registerCommand(notificationID:int, CommandClass:Class):void
		{
			if(commandInstanceList[CommandClass] == null)
			{
				var command:Object = new CommandClass();
				
				if(command is SimpleCommand)
				{
					commandInstanceList[CommandClass] = command;
				}
				else
				{
					throw("error:commandClass必须是SimpleCommand");
				}
				command = null;
			}
			
			if(commandReferenceList[notificationID]!=null)
			{
				throw("error:这个notificationID已经已经存在对应的Command了");
			}
			else
			{
				commandReferenceList[notificationID] = commandInstanceList[CommandClass];
			}
		}
		
		
		
		
		public function unRegisterCommand(notificationID:int):void
		{
			var command:SimpleCommand = commandReferenceList[notificationID];
			if(command == null)
			{
				throw("error : notificationID:"+notificationID+"没有注册过Command。不能unRegisterCommand");
			}
			else 
			{
				commandReferenceList[notificationID] = null;
				
				var find:Boolean = false;
				for each (var item:SimpleCommand in commandReferenceList) 
				{
					if(item == command)
					{
						find = true;
						break;
					}
				}
				
				if(!find)
				{
					//commandReferenceList中没有找到command实例，
					//也就是说，这个command已经没有notificationID引用了
					//在commandInstanceList中也删除它
					var classRef:Class = command["constructor"];
					if(classRef && commandInstanceList[classRef])
					{
						commandInstanceList[classRef].dispose();
						commandInstanceList[classRef] = null;
					}
					
					classRef = null;
					item = null;
					command = null;
				}
				
			}
		}
		
		
		public function removeCommand(commandClassRef:Class):void
		{
			var command:SimpleCommand = commandInstanceList[commandClassRef];
			if( command )
			{
				for (var i:int = 0; i < commandReferenceList.length; i++) 
				{
					if(commandReferenceList[i] == command)
					{
						commandReferenceList[i] = null;
					}
				}
				commandInstanceList[commandClassRef] = null;
				command.dispose();
				command = null;
			}
			commandClassRef = null;
		}
		
		
		public function getCommand(notifictionID:int):SimpleCommand
		{
			return commandReferenceList[notifictionID];
		}
		
		public function hasCommand(notifictionID:int):Boolean
		{
			if(getCommand(notifictionID))
				return true;
			else
				return false;
		}
		
		public function sendNotification(notificationID:int, body:Object=null):void
		{
			if(commandReferenceList[notificationID] == null)
			{
				throw("error:这个eventId没有注册过Command");
			}
			else
			{
				var comm:SimpleCommand = commandReferenceList[notificationID];
				comm.execute(new SimpleNotification(notificationID,body));
			}
		}
		
	}
}

class S
{
	
}